#include "server.hpp"
#define HOST "127.0.0.1"
#define USER "root"
#define PASSWD ""
#define DBNAME "gobang"
#define PORT 3306

void json_test()
{
    json_util json;
    //序列化测试
    Json::Value root;
    root["姓名"] = "刘备";
    root["年龄"] = 20;
    root["成绩"].append(60.5);
    root["成绩"].append(70);
    root["成绩"].append(90.5);
    std::string str;
    json.Serialize(root,str);
    std::cout << str << std::endl;

    //反序列化测试
    Json::Value val;
    json.DeSerialize(str,val);
    std::cout << "姓名：" << root["姓名"].asString() << std::endl;
    std::cout << "年龄：" << root["年龄"].asInt() << std::endl;
    std::cout << "成绩：";
    for(int i = 0 ; i < root["成绩"].size() ; ++i)
    {
        std::cout << root["成绩"][i] << " ";
    }
    std::cout << std::endl;
}
void split_test()
{
    std::string str = "Hello World   Is Ok  ";
    std::vector<std::string> array;
    string_util::split(str," ",array);
    for(int i = 0 ; i < array.size() ; ++i)
    {
        std::cout << array[i] << std::endl;
    }
}
void file_test()
{
    std::string body;
    file_util::read("test.txt",body);
    std::cout << body << std::endl;
}

void db_test()
{
    // 1、测试insert接口
    user_table user(HOST,USER,PASSWD,PORT,DBNAME);
    Json::Value root;
    root["username"] = "李四";
    root["password"] = "123123";

    // 2、测试login接口
    // user.login(root);
    // std::cout << root["username"] << std::endl;
    // std::cout << root["password"] << std::endl;
    // std::cout << root["score"] << std::endl;
    // std::cout << root["total_count"] << std::endl;
    // std::cout << root["win_count"] << std::endl;

    //3、测试select_by_username接口
    // user.select_by_name("李四",root);
    // std::cout << root["username"] << std::endl;
    // std::cout << root["password"] << std::endl;
    // std::cout << root["score"] << std::endl;
    // std::cout << root["total_count"] << std::endl;
    // std::cout << root["win_count"] << std::endl;

    //4、测试select_by_id接口
    // user.select_by_id(1,root);
    // std::cout << root["username"] << std::endl;
    // std::cout << root["password"] << std::endl;
    // std::cout << root["score"] << std::endl;
    // std::cout << root["total_count"] << std::endl;
    // std::cout << root["win_count"] << std::endl;
    
    //5、测试win接口
    // user.win(1);
    // user.select_by_id(1,root);
    // std::cout << root["username"] << std::endl;
    // std::cout << root["password"] << std::endl;
    // std::cout << root["score"] << std::endl;
    // std::cout << root["total_count"] << std::endl;
    // std::cout << root["win_count"] << std::endl;

    //6、测试lose接口
    user.lose(1);
    user.select_by_id(1,root);
    std::cout << root["username"] << std::endl;
    std::cout << root["password"] << std::endl;
    std::cout << root["score"] << std::endl;
    std::cout << root["total_count"] << std::endl;
    std::cout << root["win_count"] << std::endl;
}
void online_test()
{
    online_user_manage online;
    wsserver_t::connection_ptr conn;
    online.enter_game_room(2,conn);
    if(online.is_in_game_room(2))
    {
        DLOG("the user is in game room");
    }
    else 
    {
        DLOG("the user isn't in game room");
    }
    online.exit_game_room(2);
    if(online.is_in_game_room(2))
    {
        DLOG("the user is in game room");
    }
    else 
    {
        DLOG("the user isn't in game room");
    }
}

void test_room()
{
    user_table tb(HOST,USER,PASSWD,PORT,DBNAME);
    online_user_manage online;
    room r(111,&tb,&online);
    r.add_black_user(1);
    r.add_white_user(2);
    std::cout << r.get_black_id() << " " << r.get_white_id() << std::endl;
}
void test_room_manager()
{
    user_table tb(HOST,USER,PASSWD,PORT,DBNAME);
    online_user_manage online;
    wsserver_t::connection_ptr conn;
    online.enter_game_hall(10,conn);
    online.enter_game_hall(20,conn);
    
    room_manager rm(&tb,&online);
    room_ptr r = rm.create_room(10,20);
    matcher mc(&rm,&online,&tb);
    sleep(100);
}

void test_session_manager()
{
    wsserver_t ws;
    session_manager ssm(&ws);
    session_ptr ssp = ssm.create_session(10,LOGIN);
    ssm.set_session_expire_time(ssp->ssid(),5000);
    for(int i = 0 ; i < 10 ; ++i)
    {
        sleep(1);
        std::cout << std::endl;
    }
}

int main()
{
    gobang_server svr(HOST,USER,PASSWD,PORT,DBNAME);
    int port;
    std::cin >> port;
    std::cout << "启动成功！"<< std::endl;
    svr.start(port);

    return 0;
}